package com.creditease.mobile.rss.ui;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.creditease.mobile.R;
import com.creditease.mobile.rss.dao.RssDB;
import com.creditease.mobile.rss.model.RssModel;
import com.creditease.mobile.rss.ui.adapter.RssSubAdapter;

import java.util.List;

public class RssSubActivity extends Activity {

    private final static String TAG = RssSubActivity.class.getName();
    private ListView listView;
    private ImageView titleView;
    private ImageView reflushView;
    private TextView titleText;
    private RssDB db;
    private RssSubAdapter adapter;
    private List<RssModel> rssList;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.rss_sub);
        // 设置Home图标
        titleView = (ImageView) findViewById(R.id.titleIcon);
        Drawable drawable = getResources().getDrawable(R.drawable.icon_home);
        titleView.setImageDrawable(drawable);
        titleView.setAdjustViewBounds(true);
        titleView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                RssSubActivity.this.onBackPressed();
            }
        });
        reflushView = (ImageView) findViewById(R.id.reflushIcon);
        reflushView.setAdjustViewBounds(true);
        listView = (ListView) findViewById(R.id.subListView);
        // 当前页的标题
        titleText = (TextView) findViewById(R.id.titleText);
        titleText.setText("订阅管理");
        db = new RssDB(getApplicationContext());
        rssList = loadData();
        adapter = new RssSubAdapter(getApplicationContext(), rssList);
        // 需要放在 setAdapter之前，否则不显示
        if (rssList.isEmpty()) {
            listView.setEmptyView(findViewById(R.id.subEmptyMsg));
        }
        listView.setAdapter(adapter);
        // 打开详细页面
        listView.setOnItemClickListener(new RssItemClickListener());
    }

    private List<RssModel> loadData() {
        return db.findAll(true);
    }

    private final class RssItemClickListener implements OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            RssModel item = (RssModel) parent.getItemAtPosition(position);
            Log.d(TAG, "isSub=" + item.isSub);
            item.isSub = !item.isSub;
            Log.d(TAG, "isSub:" + item.isSub);
            db.updateById(item);
            adapter.notifyDataSetChanged();
//            if (item.isSub) {
//                Toast.makeText(getApplication(), "订阅：" + item.title, Toast.LENGTH_SHORT).show();
//            } else {
//                Toast.makeText(getApplication(), "取消订阅：" + item.title, Toast.LENGTH_SHORT).show();
//            }
        }
    }

    @Override
    protected void onPause() {
       HomeActivity.updateRss = true;
//        Log.d(TAG,"on pause");
        super.onPause();
    }
}